<?php
/**
 * DouYinFilter.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/22- 7:18 下午
 */

namespace common\drivers\cloudFilter;


use common\constants\PlatformConst;
use common\constants\ServerConst;
use common\drivers\interfaces\CloudFilterInterface;
use douyin\services\ToolsService;

class DouYinFilter extends BaseFilter implements CloudFilterInterface
{


    /**
     * 产品字段映射云库关系
     * @var array
     */
    protected $ProductFieldMappingToCloudProduct = [
        'name' => 'title',
        'third_cid' => 'cid',
        'product_id' => 'num_iid',
        'img' => 'main_pic',
        'platform' => 'platform',
        'server_id' => 'server_id',
        'create_time' => 'created_at',
        'update_time' => 'updated_at',
    ];

    /**
     * 抖音平台扩展字段
     * @var array
     */
    protected $DyExtraField = [
        'market_price' => 0,//划线价，单位分
        'status' => 0,//商品上下架状态：0上架 1下架
        'spec_id' => 0,//该商品使用的规格组ID
        'check_status' => 0,//商品审核状态：1未提审 2审核中 3审核通过 4审核驳回 5封禁
        'mobile' => '',//客服号码
        'first_cid' => 0,//一级类目
        'second_cid' => 0,//二级类目
        'third_cid' => 0,//三级类目
        'pay_type' => 0,//支持的支付方式：0货到付款 1在线支付 2两者都支持
        'recommend_remark' => '',//商家推荐语
        'extra' => '',//扩展字段
    ];

    public function __construct($request)
    {
        if (isset($request['product'])) {
            $this->setRequest($request['product']);
        }
    }


    /**
     *
     * @inheritDoc
     */
    public function setRequest(array $request)
    {
        $this->request = array_merge(['platform' => $this->getPlatform(), 'server_id' => $this->getServerId()], $request);
    }

    /**
     * @inheritDoc
     */
    public function getRequest(): array
    {
        return $this->request;
    }

    /**
     * @inheritDoc
     */
    public function getResponse(): array
    {
        return $this->response;
    }

    /**
     * @inheritDoc
     */
    public function getPlatform(): int
    {
        return PlatformConst::PLATFORM_DY;
    }

    /**
     * @return int
     */
    public function getServerId(): int
    {
        return ServerConst::DOUYIN_SERVER;
    }

    /**
     * @inheritDoc
     */
    public function filterToCloudProduct()
    {
        $this->response = $this->productFields;
        $request = $this->getRequest();
        foreach ($this->ProductFieldMappingToCloudProduct as $dy_column => $cloud_column) {
            $this->response[$cloud_column] = $request[$dy_column] ?? '';
        }
        $this->response['created_at'] = $this->response['created_at'] ? date("Y-m-d H:i:s",strtotime($this->response['created_at'])):"";
        $this->response['updated_at'] = $this->response['updated_at'] ? date("Y-m-d H:i:s",strtotime($this->response['updated_at'])):"";
        $response['id'] = $request['product_id'] ?? '';
        if (isset($request['pic']) || isset($request['spec_prices'])) {//详情
            $this->response['details'] = $this->productDetailFields;
            $this->response['details']['pics'] = $request['pic'] ?? [];
            $this->response['details']['product_id'] = $response['id'];
            $this->response['details']['pc_desc'] = $request['description'] ?? '';
            $this->response['details']['m_desc'] = $request['description'] ?? '';
            $this->setSkuImgAndSku($request);
            $this->setAttr($request);
        }
        $this->setExtraData($request);
    }

    /**
     * 平台扩展字段设置
     * @param $request
     */
    protected function setExtraData($request)
    {
        foreach ($this->DyExtraField as $column => $value) {
            if (isset($request[$column])) {
                $this->response['extra_data'][$column] = $request[$column];
            }
        }
    }

    /**
     * set attr
     * @param $request
     */
    protected function setAttr($request)
    {
        $product_format = $request['product_format'] ?? '';
        $product_format_arr = $product_format ? json_decode($product_format, true) : [];
        $attrs = [];
        foreach ($product_format_arr as $name => $value) {
            $attrs[] = ['key' => $name, 'value' => $value];
        }
        $this->response['details']['attr'] = $attrs;
    }

    /**
     * set skuImg
     * @param $request
     */
    protected function setSkuImgAndSku($request)
    {
        $spec_pics = $request['spec_pics'] ?? [];
        $specs = $request['specs'] ?? [];
        $spec_data = [];
        foreach ($specs as $spec) {
            $values = $spec['values'] ?? [];
            foreach ($values as $value) {
                $spec_data[$value['id']] = ['name' => $value['name'], 'parent_name' => $spec['name'], 'parent_id' => $spec['id']];
            }
        }
        $new_spec_pics = [];
        foreach ($spec_pics as $spec_pic) {
            $_spec_data = $spec_data[$spec_pic['spec_detail_id']] ?? [];
            if (!$_spec_data) {
                continue;
            }
            $new_spec_pics[] = [
                'name' => $_spec_data['parent_name'],
                'value' => $_spec_data['name'],
                'skuImg' => $spec_pic['pic'],
                'key' => $_spec_data['parent_id'] . ":" . $spec_pic['spec_detail_id'],
            ];
        }
        $this->response['details']['sku_img'] = $new_spec_pics;

        $skus = [];
        foreach ($request['spec_prices'] as $spec_price) {
            $spec_detail_data = [];
            $spec_detail_data_name = [];
            foreach ($spec_price['spec_detail_ids'] as $spec_detail_id) {
                $_spec_data = $spec_data[$spec_detail_id] ?? [];
                if (!$_spec_data) {
                    continue;
                }
                $spec_detail_data[] = $_spec_data['parent_id'] . ":" . $spec_detail_id;
                $spec_detail_data_name[] = $_spec_data['parent_name'] . ":" . $_spec_data['name'];
            }
            if ($spec_detail_data) {
                $skus[] = [
                    'product_id' => $request['product_id'] ?? '',
                    'skuId' => $spec_price['sku_id'],
                    'stock' => $spec_price['stock_num'],
                    'price' => ToolsService::moneyToYuan($spec_price['price']),
                    'properties' => $spec_detail_data,
                    'properties_name' => $spec_detail_data_name,
                ];
            }
        }
        $this->response['skus'] = $skus;
    }
}